package com.example.utils;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @Author: 马宇航
 * @Todo: 统计一次请求消耗的时间
 * @DateTime: 22/02/15/0015 上午 11:12
 * @Component: 成都蜗牛学苑
 **/
@WebFilter(urlPatterns = "/*",filterName = "timeFilter")
@Component
public class TimeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long start = System.currentTimeMillis();
        System.out.println("进入过滤器的时间"+start+"ms");
        //过滤器放行，然后，才进入到拦截器中！
        chain.doFilter(request,response);
        System.out.println("请求耗时："+(System.currentTimeMillis()-start)+"ms");
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
